package com.leyou.item.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.leyou.common.pojo.PageResult;
import com.leyou.item.pojo.Brand;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("brand")
public class BrandController {
    @Autowired
    private BrandService brandService;
    @GetMapping("page")
    public ResponseEntity<PageResult<Brand>> queryBrandsByPage(
            @RequestParam(value = "key",required = false)String key,
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows,
            @RequestParam(value = "sortBy",required = false)String sortBy,
            @RequestParam(value = "desc",required = false)Boolean desc
    ){
            PageResult<Brand> brandPageResult = brandService.queryBrandsByPage(key, page, rows, sortBy, desc);
            if(brandPageResult==null ||CollectionUtils.isEmpty(brandPageResult.getItems())){
                return ResponseEntity.notFound().build();
            }else{
                return ResponseEntity.ok(brandPageResult);
    }
    }
    @PostMapping
    public ResponseEntity<Void> insertBrand(Brand brand,@RequestParam("cids") List<Long> cids){
        System.out.println(brand.getName()+"---------------------------------------------");
        this.brandService.insertBrand(brand, cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    @PutMapping
    public ResponseEntity<Void> updateBrand(Brand brand,@RequestParam("cids") List<Long> cids){
        System.out.println(brand.getName()+"---------------------------------------------");
        this.brandService.updateBrand(brand, cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    @PostMapping("bid/{bid}")
    public ResponseEntity<Void> deleteBrand(@PathVariable("bid") Long bid){
        System.out.println(bid+"---------------------------------------------");
        brandService.deleteBrand(bid);
        return ResponseEntity.status(HttpStatus.OK).build();
    }
    @GetMapping("cid/{cid}")
    public ResponseEntity<List<Brand>> getBrandByCid(@PathVariable("cid") Long cid){
        List<Brand> brandsList = brandService.getBrandsList(cid);
        if(CollectionUtils.isEmpty(brandsList)){
            return ResponseEntity.notFound().build();
        }else{
            return ResponseEntity.ok(brandsList);
        }
    }}
